﻿using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using WebApplication1.classes.Core.JSTree;
using WebApplication1.classes.Core.Layout;

namespace WebApplication1.classes.dataSources {

    public class LayoutDatasource {
        private Layout dataset_;
        public String filePath{get;set;}

        public LayoutDatasource(){
            dataset_ = null;
        }

        public void deserialize(){
            dataset_ = null;
            if (String.IsNullOrEmpty(filePath)){
                return;
            }
            using ( var sr = new StreamReader( filePath ) ) {
                String json = sr.ReadToEnd();
                var jsonSerializerSettings = new JsonSerializerSettings();
                jsonSerializerSettings.NullValueHandling = NullValueHandling.Ignore;
                dataset_ = JsonConvert.DeserializeObject<Layout>( json, jsonSerializerSettings );
            }
        }

        private void parseLayoutIntoJsTreeNode(Layout layout, JsTreeNode node){
            foreach ( Layout childLayout  in layout.children/*.copyChildrenAsList()*/ ) {
                var childJsTreeNode = new JsTreeNode(childLayout.name);
                node.children.Add(childJsTreeNode);
                childJsTreeNode.attr.id = childLayout.uniqueIdentifier;
                parseLayoutIntoJsTreeNode(childLayout,childJsTreeNode);
            }
        }

        public IList<Layout> layoutChildListForItemPath(string itemPath){
            return dataset_.childrenListForUniqueKey(itemPath);
        }

        public string layoutJsTreeForItemPath(string id){

            string result = null;
            var jsTree = new JsTree();

            
            IList<Layout> list = dataset_.childrenListForUniqueKey(id);
            foreach(var item in list){
                var node = new JsTreeNode(item.name);
                node.attr.id = item.uniqueIdentifier;
                parseLayoutIntoJsTreeNode(item,node);
                jsTree.data.Add(node);
            }

            
            result = jsTree.data.jsonString();
            return String.Format("{0}",result);
        }

        public bool deleteItemPath(string itemPath){
            Layout itemByUniqueId = dataset_.itemByUniqueId(itemPath);
            if (itemByUniqueId == null) 
                return false;

            Layout parent = itemByUniqueId.parent() ?? dataset_;

            return parent.children.Remove(itemByUniqueId);
        }

        public bool insertItemAtPosition(string parentPath, string itemName, int position){
            Layout parent = dataset_.itemByUniqueId(parentPath);
            if (parent == null) return false;

            var child = new Layout(itemName);
            parent.children.Add(child);
            child.setParent(parent);


            return true;
        }

        public bool moveItemAtPosition(Layout parent, Layout child, int position){
            
            return true;
        }


        public void renameItem(string itemPath, string newName){
            Layout item = dataset_.itemByUniqueId(itemPath);
            item.name = newName;
        }
    }
}